
/* Globals */
var theForm ;
var gParams = window.opener.dlgParams ;
var onLoadHookFunc = null;
function _onLoadDefault ()
{
	theForm = document.theForm;
	if ( gParams ) gParams.isOpen = true ;
	focusFirstElement();
	if ( isNav ) document.captureEvents( Event.KEYDOWN );
	document.onkeydown = _onKeyDefault ;
	if ( onLoadHookFunc ) onLoadHookFunc();
}	
function _onUnLoadDefault ()
{
	if ( gParams ) 
	{
		gParams.isOpen = false ;
		if ( gParams.bCloseOpenerOnClose && window.opener )
		{
			window.opener.close();
		}
	}
}	
// handle ESCape key: simulate Cancel button
// handle ENTER/RETURN key: simulate OK button
// NOTE: Nav doesn't seem to capture the ESC key
function _onKeyDefault ( evt )
{
	var keyCode = ( isIE ) ? event.keyCode : evt.which

	switch ( keyCode )
	{
		case 27:	// ESCape
			theForm.onreset();
			return false ;
		case 13:	// ENTER
			if ( isNav )
			{
				if ( theForm.onsubmit() )
					theForm.submit();
				return false ;
			}
	}
	return true ;
}
function doDlgClose ( fOK )
{
	if ( fOK && (gParams.OKHandler != null) )
		gParams.OKHandler( formToObject( theForm, gParams.userParams ) );
	window.close();
	return true;
}
function formToObject ( f, obj )
{
	if ( ! f ) return obj ;
	if ( ! obj ) obj = new Object();
	var ctrl ;
	for ( var i = 0; i < f.elements.length; i++ )
	{
		ctrl = f.elements[ i ];
		switch( ctrl.type )
		{
			case "submit":
			case "reset":
				break ;
			default :
				obj[ ctrl.name ] = getCtrlValue( ctrl );
				break ;
		}
	}
	return obj ;	
}
function focusFirstElement ()
{
	if ( ! theForm ) return ;
	for ( var i = 0 ; i < theForm.elements.length ; i++ )
	{
		var ctrl = theForm.elements[ i ];
		switch( ctrl.type )
		{
			case "text" :
			case "textarea" :
			case "password" :
			case "select-one" :
			case "select-multiple" :
			case "checkbox":
			case "radio":
			case "submit":
			case "reset":
				if ( isIE && ( ctrl.readOnly || ctrl.disabled ) )
					break ;
				ctrl.focus();	
				return ;
			default:
				break ;
		}	// swtich( ctrl.type )
	}
}

